/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* Installer.java
*
* Created on December 1, 2007, 9:15 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
 */
package main.genomemap;

import db.genomemap.RDBSettings;
import db.gui.RDBConnectionModel;

import fs.genomemap.FileSettings;

import genomemap.data.ChData;

import gui.genomemap.inputModel.AnnealIModel;
import gui.genomemap.inputModel.ComGLHoodIModel;
import gui.genomemap.inputModel.ComJLHoodIModel;
import gui.genomemap.inputModel.ComPLHoodIModel;
import gui.genomemap.inputModel.ProgCurveIModel;
import gui.genomemap.outputModel.AnnealOModel;
import gui.genomemap.outputModel.ComGLHoodOModel;
import gui.genomemap.outputModel.ComJLHoodOModel;
import gui.genomemap.outputModel.ComPLHoodOModel;
import gui.genomemap.outputModel.ProgCurveOModel;

import javautil.collections.ArrayUtil;

import java.util.Collection;
import java.util.HashSet;
import java.util.Map;

/**
 * file and settings needed before the application runs.
 * This will be a one-time job.
 * @author stewari1
 */
public class Installer {

    /** Creates a new instance of Installer */
    public Installer() {}

    /**
     * Method description
     */
    private static void installFiles() {

        new Preferences().commit();
        installFileSettings();
        installRDBSettings();
    }

    /**
     * Method description
     */
    private static void installFileSettings() {


        // install all the views in file settings
        FileSettings fileSettings = new FileSettings();


        // add theChData map
        fileSettings.createChDataMap();
        fileSettings.createExpProgCurveMap();
        fileSettings.createGeneSetMap();
        fileSettings.createProbeSetMap();
        fileSettings.createCloneSetMap();


        // add outputmodels collection path (.obj file paths)
        fileSettings.addOutputModelCollectionPath(ComGLHoodOModel.class);
        fileSettings.addOutputModelCollectionPath(ComPLHoodOModel.class);
        fileSettings.addOutputModelCollectionPath(ComJLHoodOModel.class);
        fileSettings.addOutputModelCollectionPath(AnnealOModel.class);
        fileSettings.addOutputModelCollectionPath(ProgCurveOModel.class);
        fileSettings.commit();


        // restart fileSettings and create sample output models
        fileSettings = FileSettings.load();

        fileSettings.saveOutputModels(getSampleComGLHoodOModelCollection());
        fileSettings.saveOutputModels(getSampleComPLHoodOModelCollection());
        fileSettings.saveOutputModels(getSampleComJLHoodOModelCollection());
        fileSettings.saveOutputModels(getSampleAnnealOModelCollection());
        fileSettings.saveOutputModels(getProgCurveOModelCollection());
    }

    /**
     * Method description
     */
    private static void installRDBSettings() {

        RDBSettings rdbSettings = new RDBSettings();


        // create a default connection model
        RDBConnectionModel defaultConnectionModel = new RDBConnectionModel();

        defaultConnectionModel.setServerType(RDBSettings.SERVER_TYPE_MYSQL);
        defaultConnectionModel.setServerHost("localhost");
        defaultConnectionModel.setUserName("root");
        defaultConnectionModel.setPassword("kalpana90y");
        defaultConnectionModel.setSchema("gene1");
        rdbSettings.setDefaultConnectionModel(defaultConnectionModel);
        rdbSettings.commit();
    }

    public static void main(String[] args) {
        Installer.installFiles();
    }

    /**
     * Method description
     *
     * @return description
     */
    private static Collection<ComGLHoodOModel> getSampleComGLHoodOModelCollection() {

        Collection<ComGLHoodOModel> sampleComGLHoodOutputModels = new HashSet<>();
        Map<Integer, ChData> chDataMap                          =
            FileSettings.load().getChDataMap();

        for (int index = 0; index < 7; index++) {

            ChData chData              = chDataMap.get(new Integer(index + 1));
            ComGLHoodIModel inputModel = new ComGLHoodIModel();

            inputModel.setLinkageGroup(index + 1);
            inputModel.setOrder(ArrayUtil.IntegerSequence(chData.getGeneCount(), false));

            ComGLHoodOModel outputModel = new ComGLHoodOModel();

            outputModel.setInputModel(inputModel);
            outputModel.setLHood(0.0);
            sampleComGLHoodOutputModels.add(outputModel);
        }

        return sampleComGLHoodOutputModels;
    }

    /**
     * Method description
     *
     * @return description
     */
    private static Collection<ComPLHoodOModel> getSampleComPLHoodOModelCollection() {

        Collection<ComPLHoodOModel> sampleComPLHoodOutputModels = new HashSet<>();
        Map<Integer, ChData> chDataMap                          =
            FileSettings.load().getChDataMap();

        for (int index = 0; index < 7; index++) {

            ChData chData              = chDataMap.get(new Integer(index + 1));
            ComPLHoodIModel inputModel = new ComPLHoodIModel();

            inputModel.setLinkageGroup(index + 1);
            inputModel.setOrder(ArrayUtil.IntegerSequence(chData.getProbeCount(), false));

            ComPLHoodOModel outputModel = new ComPLHoodOModel();

            outputModel.setInputModel(inputModel);
            outputModel.setLHood(0.0);
            sampleComPLHoodOutputModels.add(outputModel);
        }

        return sampleComPLHoodOutputModels;
    }

    /**
     * Method description
     *
     * @return description
     */
    private static Collection<ComJLHoodOModel> getSampleComJLHoodOModelCollection() {

        Collection<ComJLHoodOModel> sampleComJLHoodOutputModels = new HashSet<>();
        Map<Integer, ChData> chDataMap                          =
            FileSettings.load().getChDataMap();

        for (int index = 0; index < 7; index++) {

            ChData chData              = chDataMap.get(new Integer(index + 1));
            ComJLHoodIModel inputModel = new ComJLHoodIModel();

            inputModel.setLinkageGroup(index + 1);
            inputModel.setOrder(ArrayUtil.IntegerSequence(chData.getProbeCount(), false));

            ComJLHoodOModel outputModel = new ComJLHoodOModel();

            outputModel.setInputModel(inputModel);
            outputModel.setLHood(0.0);
            outputModel.setIterationLog("This is dummy log string..");
            sampleComJLHoodOutputModels.add(outputModel);
        }

        return sampleComJLHoodOutputModels;
    }

    /**
     * Method description
     *
     * @return description
     */
    private static Collection<AnnealOModel> getSampleAnnealOModelCollection() {

        Collection<AnnealOModel> sampleAnnealOutputModels = new HashSet<>();
        Map<Integer, ChData> chDataMap                    = FileSettings.load().getChDataMap();

        for (int index = 0; index < 7; index++) {

            ChData chData           = chDataMap.get(new Integer(index + 1));
            AnnealIModel inputModel = new AnnealIModel();

            inputModel.setLinkageGroup(index + 1);
            inputModel.setOrder(ArrayUtil.IntegerSequence(chData.getProbeCount(), false));

            AnnealOModel outputModel = new AnnealOModel();

            outputModel.setInputModel(inputModel);
            outputModel.setConvValue(0.0);
            sampleAnnealOutputModels.add(outputModel);
        }

        return sampleAnnealOutputModels;
    }

    /**
     * Method description
     *
     * @return description
     */
    private static Collection<ProgCurveOModel> getProgCurveOModelCollection() {

        Collection<ProgCurveOModel> sampleProgCurveOutputModels = new HashSet<>();
        Map<Integer, ChData> chDataMap                          =
            FileSettings.load().getChDataMap();

        for (int index = 0; index < 7; index++) {

            ChData chData              = chDataMap.get(new Integer(index + 1));
            ProgCurveIModel inputModel = new ProgCurveIModel();

            inputModel.setLinkageGroup(index + 1);
            inputModel.setOrder(ArrayUtil.IntegerSequence(chData.getProbeCount(), false));

            ProgCurveOModel outputModel = new ProgCurveOModel();

            outputModel.setInputModel(inputModel);
            sampleProgCurveOutputModels.add(outputModel);
        }

        return sampleProgCurveOutputModels;
    }
}
